package com.example.bottmmenu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTabHost;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.bottmmenu.fragment.HomeFragment;
import com.example.bottmmenu.fragment.MoreFragment;
import com.example.bottmmenu.fragment.MyFragment;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private FragmentTabHost mTabHost;
    private ArrayList<Tab> mTabs = new ArrayList<>(4);
    private LayoutInflater mInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTabHost = this.findViewById(android.R.id.tabhost);
        mTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);

        initTab();
        initTabHost();
    }

    private void initTab(){
        Tab tab_chicken = new Tab(HomeFragment.class, R.string.home_page, R.drawable.chicken);
        Tab tab_drink = new Tab(MoreFragment.class, R.string.more_page, R.drawable.drink);
        Tab tab_icecream = new Tab(MyFragment.class, R.string.my_page, R.drawable.icecream);

        mTabs.add(tab_chicken);
        mTabs.add(tab_drink);
        mTabs.add(tab_icecream);
    }

    private void initTabHost() {
        mInflater = LayoutInflater.from(this);
        for (Tab tab : mTabs) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(tab.getTitle()));
            tabSpec.setIndicator(buildIndicator(tab));
            mTabHost.addTab(tabSpec, tab.getFragment(), null);
        }
        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        mTabHost.setCurrentTab(0);
    }

    private View buildIndicator(Tab tab) {
        View view = mInflater.inflate(R.layout.tab_indicator, null);
        ImageView img = view.findViewById(R.id.icon_tab);
        TextView text = view.findViewById(R.id.txt_indicator);
        img.setBackgroundResource(tab.getIcon());
        text.setText((tab.getTitle()));
        return view;
    }
}
